# -*- coding: utf-8 -*-
"""
对比：按不同类型进行比较
"""

import py_global

import logging

logger = logging.getLogger(__name__)


def compare_dict(dict1: dict, dict2: dict):
    keys1 = dict1.keys()
    keys2 = dict2.keys()
    if len(keys1) != len(keys2):
        logger.error(f'len(keys1):{len(keys1)} != len(keys2):{len(keys2)}')
        return False

    for key in keys1:
        value1 = dict1.get(key)
        value2 = dict2.get(key)

        if isinstance(value1, dict):
            if not compare_dict(value1, value2):
                return False
            pass
        elif isinstance(value1, list):
            if not compare_list(value1, value2):
                return False
        else:
            if not compare_data(value1, value2):
                return False
            pass
        pass
    return True


def compare_list(list1: list, list2: list):
    if len(list1) != len(list2):
        logger.error(f'len(list1):{len(list1)} != len(list2):{len(list2)}')
        return False
    flag = True
    for item1 in list1:
        flag = False
        for item2 in list2:
            if not isinstance(item1, type(item2)):
                continue
            if isinstance(item1, dict):
                if compare_dict(item1, item2):
                    flag = True
                    break
                pass
            elif isinstance(item1, list):
                if compare_list(item1, item2):
                    flag = True
                    break
            else:
                if compare_data(item1, item2):
                    flag = True
                    break
                pass
            pass
        if not flag:
            break
        pass
    return flag


def compare_data(data1, data2):
    if not isinstance(data1, type(data2)):
        return False

    if isinstance(data1, str) or isinstance(data1, int):
        if data1 != data2:
            return False
        pass
    elif isinstance(data1, float):
        if round(data1 - data2, 4) != 0:
            return False
        pass
    else:
        try:
            if data1 != data2:
                return False
        except Exception as e:
            logger.error(f'type(data2):{type(data2)} is not support:{repr(e)}')
            return False
    return True


if __name__ == '__main__':
    dict1 = {}
    dict2 = {
        'key': 'value'
    }
    flag = compare_dict(dict1, dict2)
    logger.info(f'flag:{flag}')
    pass
